How can I replace a package installed via APT(or any other package managers) with the version from source without violating system? My problem is the same as this link, but I think the answer is not really helpful.

For example, I am trying to build a project that requires Clang (version 12.0.0+), but the latest version of Clang (APT) is 10.0.0, so apparently I have to install Clang from source (GitHub). So I have 3 questions:

  • Should I need to remove the old Clang(APT)?
    When I tried sudo apt remove clang, the system also asked me to remove a lot of dependencies (REALLY IMPORTANT), and I think the system would corrupt if I remove Clang.
  • How can I set up the PATH so that the system will use the later version of Clang, because there are two versions of it in my laptop?
  • If a package is install via APT, it is very easy to update it via sudo apt upgrade. but how can I upgrade a package via git without reinstall it again? Every clang update takes me almost half a day to complete.
  • Yes, you are correct that you cannot remove clang because of dependencies. The path for the compiled version should already be set to /usr/local/bin by default and the system will automatically use this version. See the answer by Ray for more details. – mchid May 18 at 9:23

So, to answer your 3 questions,

  1. No, you don't need to remove the apt-installed clang. Just keep it where it is in /usr/bin/clang.

    If you download and install clang as a user, you probably have the option to put it in your home directory. If it's installed in a system-level directory, it would probably go under /usr/local/. For example, /usr/local/bin/. So, the two versions can co-exist.

  2. The $PATH variable is the easiest part. First, type env | grep PATH and see if your PATH variable has already been set for you. What you want to see is the order in which the paths appear. Earlier in the list is higher priority. It's possible that /usr/local/bin/ has already been set for you. If not, then you can set it in your ~/.bashrc file. Do something like:

    export PATH=/usr/local/bin/:"$PATH"

    somewhere in the file. Of course, if you reversed the above:

    export PATH="$PATH":/usr/local/bin/

    then you've placed /usr/local/bin/ lowest in priority. You don't want that. Other parts of the file might put things before /usr/local/bin/. That's ok as long as it remains before /usr/bin/.

    Then type source ~/.bashrc and try env | grep PATH to see if you've done it correctly. Note that this will affect not just clang but all your programs so if you had other programs in /usr/local/bin/ previously, then they will now have a higher priority over /usr/bin/.

  3. Your last point is the hardest part as there is no easy way. What you get from GitHub is usually the source code. If there are pre-built binaries, then great.

    But most likely, it's the source code and you will have to (a) compile and (b) install every time you want to update. I haven't installed clang manually before, but for some programs with a Makefile, one would do a make; make install. The first command compiles and the second installs it. Prior to this, one might set the directory to install to using ./config, cmake, etc.

My personal opinion is if you don't need the latest feature, then you rarely need to keep a compiler that up to date. So I don't think you need to re-compile clang that often... If you're developing a program for distribution, then rarely will the users also have such a new compiler.

Does this help?

  • 1
    Usually, we have to do make; sudo make install. Don't forget the sudo on the install command or it won't have permission to install to /usr/local/bin which is typically the default directory. – mchid May 18 at 8:57
  • Sorry for replying late. Thank you so much for your answers! – Minh Nhật May 20 at 18:27
  • 1
    @mchid Thanks for the clarification! Though, one can install into one's home directory as well. In that case, sudo isn't necessary. Depends what the OP chooses. – Ray May 21 at 2:53
  • 1
    I prefer to use checkinstall (it creates a deb file) for that, so I can remove the build with apt. – nobody May 21 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.