Update multiple versions of dev tools under Ubuntu
500 Words | Read in about 2 Min | View times
Overview
Since different projects may require different versions of the same dev tool, we have to install multiple versions of them and provide a way for quick switch. This post introduces how to use update-alternatives
command to switch dev tool versions under Ubuntu.
I will take protoc
for example.
- ProjectA requires protoc 3.6.1
- ProjectB requires protoc 3.17.0
Install multiple versions of protoc
- Download different releases for Protocol Buffers
3.17.0: https://github.com/protocolbuffers/protobuf/archive/refs/tags/v3.17.0.tar.gz
3.16.1: https://github.com/protocolbuffers/protobuf/archive/refs/tags/v3.6.1.tar.gz
- Compile and install Protocol Buffers respectively
protoc
will be installed under /usr/local/bin
- Rename the protoc file in different names
3.17.0 -> protoc3.17
3.6.1 -> protoc3.6
- Install them into
update-alternatives
1# install protoc3.17 with priority 170 as an alternative for protoc
2$ sudo update-alternatives --install /usr/local/bin/protoc protoc /usr/local/bin/protoc3.17 170
3
4# install protoc3.6 with priority 60 as another alternative for protoc
5$ sudo update-alternatives --install /usr/local/bin/protoc protoc /usr/local/bin/protoc3.6 60
Query protoc version
1$ sudo update-alternatives --display protoc
2
3protoc - auto mode
4 link best version is /usr/local/bin/protoc3.17
5 link currently points to /usr/local/bin/protoc3.17
6 link protoc is /usr/local/bin/protoc
7/usr/local/bin/protoc3.17 - priority 170
8/usr/local/bin/protoc3.6 - priority 60
Switch version of protoc
1$ sudo update-alternatives --config protoc
2
3There are 2 choices for the alternative protoc (providing /usr/local/bin/protoc).
4
5 Selection Path Priority Status
6------------------------------------------------------------
7* 0 /usr/local/bin/protoc3.17 170 auto mode
8 1 /usr/local/bin/protoc3.17 170 manual mode
9 2 /usr/local/bin/protoc3.6 60 manual mode
10
11Press <enter> to keep the current choice[*], or type selection number:
An alternative with a higher priority will be set as the default choice. We can enter the number listed above to switch to another version.
- Query version before switching
1$ protoc --version
2
3libprotoc 3.17.0
- Query version after switching
1$ sudo update-alternatives --config protoc
2
3There are 2 choices for the alternative protoc (providing /usr/local/bin/protoc).
4
5 Selection Path Priority Status
6------------------------------------------------------------
7* 0 /usr/local/bin/protoc3.17 170 auto mode
8 1 /usr/local/bin/protoc3.17 170 manual mode
9 2 /usr/local/bin/protoc3.6 60 manual mode
10
11Press <enter> to keep the current choice[*], or type selection number: 2
12update-alternatives: using /usr/local/bin/protoc3.6 to provide /usr/local/bin/protoc (protoc) in manual mode
13
14$ protoc --version
15
16libprotoc 3.6.1
Alias
It’s too long to enter a full update-alternatives
command to switch versions. Consider to config an alias for this command.
Open ~/.bashrc
or ~/.zshrc
, append the following line at the end:
alias switchprotoc="sudo update-alternatives --config protoc"
Save and quit, use source
command to make it works:
1$ source ~/.zshrc
Now, instead of entering the full long command, we can use the short one switchprotoc
to switch protoc version directly:
1$ switchprotoc